<?php

class Admin_noticias extends Controller{
	function Admin_noticias(){
		parent::Controller();
		//$this->output->enable_profiler(TRUE);
		$this->load->helpers(array('form'));
	}
	function index(){
		redirect('/admin_noticias/listar', 'refresh');
	}
	
	function listar(){
		$this->load->helper('text');
		$data['configs'] = $this->configs_model->getConfigs();
		if(!$this->usuarios_model->logado()){
			redirect('/admin/login', 'refresh');
		}
		//carrega o modelo menus e pega os menus com seus itens
		$data['menus'] =  $this->menus_model->getTodososMenus();
		$data['menusitens'] = $this->menus_model->getTodososMenusItens();
		
		$this->load->model("noticias_model");
		$data['noticias'] = $this->noticias_model->getNoticias();

		//mostra o layout
		$this->load->view($data['configs']->clayout.'/admin/noticias', $data);
	}
	
	function add(){
		$data['configs'] = $this->configs_model->getConfigs();
		if(!$this->usuarios_model->logado()){
			redirect('/admin/login', 'refresh');
		}

		//carrega o modelo menus e pega os menus com seus itens
		$data['menus'] =  $this->menus_model->getTodososMenus();
		$data['menusitens'] = $this->menus_model->getTodososMenusItens();
		
		$this->load->library('validation');

		$rules['titulo']	= "trim|required";
		$rules['ativa']		= "trim|required";
		$this->validation->set_rules($rules);
		$fields['titulo']	= 'titulo';
		$fields['ativa']	= 'ativa';

		$this->validation->set_fields($fields);
		
		//mostra o layout
		$this->load->view($data['configs']->clayout.'/admin/noticiasadd', $data);
	}
	
	function save(){
		$data['configs'] = $this->configs_model->getConfigs();
		if(!$this->usuarios_model->logado()){
			redirect('/admin/login', 'refresh');
		}

		//carrega o modelo menus e pega os menus com seus itens
		$data['menus'] =  $this->menus_model->getTodososMenus();
		$data['menusitens'] = $this->menus_model->getTodososMenusItens();

		$this->load->library('validation');
		$this->load->model('noticias_model');
		switch ($this->input->post('acao')) {
			case 'salvar':
			$rules['titulo']	= "trim|required";
			$rules['ativa']		= "trim|required";
			$this->validation->set_rules($rules);
			$fields['titulo']	= 'titulo';
			$fields['ativa']	= 'ativa';
		
			$this->validation->set_fields($fields);
			if ($this->validation->run() == FALSE) {
				$this->load->view($data['configs']->clayout.'/admin/noticiasadd', $data);
			} else {
				if($this->noticias_model->salvarNoticia($this->input->post('titulo'), $this->input->post('texto'), $this->input->post('data'), $this->input->post('autor'), $this->input->post('ativa'))){
					redirect('/admin_noticias', 'refresh');
				}
			}
			break;
			
			case 'editar':
			$rules['id']		= "trim|required";
			$rules['titulo']	= "trim|required";
			$rules['ativa']		= "trim|required";
			$this->validation->set_rules($rules);
			$fields['id']	= 'id';
			$fields['titulo']	= 'titulo';
			$fields['ativa']	= 'ativa';
		
			$this->validation->set_fields($fields);
			if ($this->validation->run() == FALSE) {
				$data['noticia'] = $this->noticias_model->getNoticia($this->input->post('id'));
				$this->load->view($data['configs']->clayout.'/admin/noticiasedit', $data);
			} else {
				if($this->noticias_model->editarNoticia($this->input->post('id'), $this->input->post('titulo'), $this->input->post('texto'), $this->input->post('data'), $this->input->post('autor'), $this->input->post('ativa'))){
					redirect('/admin_noticias', 'refresh');
				}
			}
			break;
			
			case 'deletar':
			if($this->noticias_model->deletarNoticia($this->input->post('id'))){
				redirect('/admin_noticias', 'refresh');
			}
			break;
			
			default:
				show_error("Erro ao tentar salvar a noticia.");
			break;
		}
	}

	function edit($id){
		$this->load->helpers(array('form'));	
		$data['configs'] = $this->configs_model->getConfigs();
		if(!$this->usuarios_model->logado()){
			redirect('/admin/login', 'refresh');
		}

		$this->load->model('noticias_model');
		$data['noticia'] = $this->noticias_model->getNoticia($id);

		//carrega o modelo menus e pega os menus com seus itens
		$data['menus'] =  $this->menus_model->getTodososMenus();
		$data['menusitens'] = $this->menus_model->getTodososMenusItens();
		
		//mostra o layout
		$this->load->view($data['configs']->clayout.'/admin/noticiasedit', $data);
	}

	function del($id){
		$this->load->helpers(array('form'));
		
		$data['configs'] = $this->configs_model->getConfigs();
		if(!$this->usuarios_model->logado()){
			redirect('/admin/login', 'refresh');
		}

		$this->load->model('noticias_model');
		$data['noticia'] = $this->noticias_model->getNoticia($id);

		//carrega o modelo menus e pega os menus com seus itens
		$data['menus'] =  $this->menus_model->getTodososMenus();
		$data['menusitens'] = $this->menus_model->getTodososMenusItens();
		
		if($data['noticia']->ativa == 1){
			$data['noticia']->ativa = 'Ativo';
		}else{
			$data['noticia']->ativa = 'Desativo';
		}

		//mostra o layout
		$this->load->view($data['configs']->clayout.'/admin/noticiasdel', $data);
	}
	
}

?>